package com.turing.post.tuxedo.util;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Element;

import com.turing.post.util.PCPSApplicationException;

/**
 * 支付卡对象类
 * 
 * @author yxb
 * 
 */
@SuppressWarnings("serial")
public class PaidObj implements Serializable {
	/**
	 * 预付卡合计发生额(分)
	 */
	public BigInteger totalAmount = BigInteger.ZERO;

	/**
	 * 预付卡张数
	 */
	public BigInteger cardNum = BigInteger.ZERO;

	/**
	 * 具体的卡支付信息
	 */
	public List<CardPaidObj> cplist = new ArrayList<CardPaidObj>();

	/**
	 * 解析Paid Section对象
	 * 
	 * @param node
	 * @throws Exception
	 */
	public void parsePaidObj(Element node) throws PCPSApplicationException {
		// 预付卡合计发生额(分)
		totalAmount = TransXmlHelper.getInteger(node,
				TransXmlHelper.XML_NODE_TotalAmount);
		if (totalAmount.compareTo(BigInteger.ZERO) < 0)
			ParaseException.TransParamException("TotalAmount["
					+ totalAmount.toString() + "] invalid!");

		// 预付卡张数
		cardNum = TransXmlHelper.getInteger(node,
				TransXmlHelper.XML_NODE_CardNum);
		if (cardNum.compareTo(BigInteger.ONE) < 0)
			ParaseException.TransParamException("CardNum[" + cardNum.toString()
					+ "] invalid!");

		CardPaidObj cp = null;

		// 获取所有的节点
		List<Element> cpnodes = node.getChildren(TransXmlHelper.XML_NODE_Card);
		if (cpnodes == null || cpnodes.size() != cardNum.intValue())
			ParaseException.TransParamException("invalid Paid.Card section!");

		BigInteger allamt = BigInteger.ZERO;

		// 卡支付信息
		for (Element cpnode : cpnodes) {
			cp = new CardPaidObj();
			cp.parseCardPaidObj(cpnode);
			allamt = allamt.add(cp.amount);
			cplist.add(cp);
		}

		// 校验合计数
		if (totalAmount.compareTo(allamt) != 0)
			ParaseException.TransParamException("totalAmount["
					+ totalAmount.toString() + "] <> All Amount["
					+ allamt.toString() + "]!");
	}
}
